package com.jiamao.goplay.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.jiamao.lib.view.LoadDialog;


/**
 * 
 * @ClassName BaseActivity 
 * @Description TODO(这里用一句话描述这个类的作用) 
 * @author 王博扬
 * @date 2015-12-3 上午11:39:03 
 * @history
 * 1.YYYY-MM-DD
 *    author:
 *    description:
 */
@SuppressLint("NewApi")
public class BaseMVCActivity extends AppCompatActivity {

	//public RxManger rxManger = new RxManger();
	private LoadDialog mProgressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO Auto-generated method stub

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		//rxManger.clear();
		dismissLoadingDialog();

	}


	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		//StatService.onPause(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//StatService.onResume(this);
	}
	/**
	 * 防止窗体多次执行
	 */
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void showLoadingDialog() {
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			return;
		}
		if(isFinishing()){
			return;
		}
		//String title = getResources().getString(R.string.dialog_title_waitting);
		try {
			if (mProgressDialog == null) {
				mProgressDialog = new LoadDialog(this,"加载中...");
				mProgressDialog.show();
			} else if (!mProgressDialog.isShowing()) {
				mProgressDialog.show();
			}
		} catch (Exception e) {
			Log.e("BaseActivity", "progressDialog error", e);
		}
	}

	public void dismissLoadingDialog() {
		if (!isFinishing() && mProgressDialog != null
				&& mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
		}
		mProgressDialog = null;
	}

	public void showInputMethodWindow(final EditText editContent) {

		InputMethodManager im = (InputMethodManager) editContent
				.getContext().getSystemService(
						Context.INPUT_METHOD_SERVICE);
		im.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

	}

	public void hideInputMethodWindow(final EditText editContent) {

		InputMethodManager im = (InputMethodManager) editContent
				.getContext().getSystemService(
						Context.INPUT_METHOD_SERVICE);
		im.hideSoftInputFromWindow(editContent.getWindowToken(), 0);

	}

	protected void hideInput() {

		InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (im != null && getCurrentFocus() != null) {
			im.hideSoftInputFromWindow(getCurrentFocus()
							.getApplicationWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
		}

	}

	public void hideSoftInputView() {

		// TODO Auto-generated method stub
		InputMethodManager manager = ((InputMethodManager) BaseMVCActivity.this
				.getSystemService(Activity.INPUT_METHOD_SERVICE));
		if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
			if (getCurrentFocus() != null)
				manager.hideSoftInputFromWindow(getCurrentFocus()
								.getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
		}

	}

}
